//
//  NSArray+sortAndRemoveRepeat.m
//  MengLiao
//
//  Created by xiuxin on 2022/7/15.
//  Copyright © 2022 BaseBusiness. All rights reserved.
//



@implementation NSArray (sortAndRemoveRepeat)

+ (NSMutableArray *)sortAndRemoveRepeatWithOldArr:(NSArray *)firstArr addArr:(NSArray *)secondArr sortCompareBlock:(BOOL (^)(id sA, id sB))sortCompare repeatCompare:(BOOL(^)(id rA, id rB))repeatCompare
{
    NSMutableArray * resuleArray   = [NSMutableArray array];
    if (!secondArr || secondArr.count == 0) {
        [resuleArray addObjectsFromArray:firstArr];
        return  resuleArray;
    }else if (!firstArr || firstArr.count == 0){
        [resuleArray addObjectsFromArray:secondArr];
        return resuleArray;
    }
    
    NSMutableArray *repeatArr = [NSMutableArray array];
    if (repeatCompare) {//进行去重操作
        for (int i = 0; i < firstArr.count; i++) {
            BOOL isAllNoRepeat = YES;
            for (int j = 0; j < secondArr.count; j++) {
                BOOL isRepeat = repeatCompare(firstArr[i], secondArr[j]);
                if (isRepeat) {//有重复
                    isAllNoRepeat = NO;
                    break;
                }
            }
            if (isAllNoRepeat) {
                [repeatArr addObject:firstArr[i]];
            }
        }
        [resuleArray addObjectsFromArray:repeatArr];
        [resuleArray addObjectsFromArray:secondArr];
    }
    if (sortCompare == nil) {//不需要排序
        return resuleArray;
    }else{//需要根据sortBlock进行排序 (排序必须先经过去重)
        [resuleArray removeAllObjects];
        NSInteger i = 0;
        NSInteger j = 0;
        while (i < [repeatArr count] && j < [secondArr count] ) {
            //如果block 返回值为YES 降序排列, NO升序排列
            BOOL isAscendingOrder = sortCompare(repeatArr[i] ,secondArr[j]);
            if (isAscendingOrder) {//
                resuleArray[i + j] = repeatArr[i];
                i++;
            }else{
                resuleArray[i + j] = secondArr[j];
                j++;
            }
        }
        //保证数组剩余的部分加入合并后的数组中
        while (i < [repeatArr count]) {
            resuleArray[i + j] = repeatArr[i];
            i++;
        }
        while (j < [secondArr count]) {
            resuleArray[i + j] = secondArr[j];
            j++;
        }
        return resuleArray;
    }
}

+ (NSMutableArray *)removeRepeatWithOldArr:(NSMutableArray *)firstArr addArr:(NSArray *)secondArr repeatCompare:(BOOL(^)(id rA, id rB))repeatCompare
{
    NSMutableArray * resuleArray   = [NSMutableArray array];
    if (!secondArr || secondArr.count == 0) {
        return  firstArr;
    }else if (!firstArr || firstArr.count == 0){
        [firstArr addObjectsFromArray:secondArr];
        return firstArr;
    }
    
    NSMutableArray *repeatArr = [NSMutableArray array];
    if (repeatCompare) {//进行去重操作
        for (int i = 0; i < firstArr.count; i++) {
            NSInteger repeatIndex = -1;
            for (int j = 0; j < secondArr.count; j++) {
                
                BOOL isRepeat = repeatCompare(firstArr[i], secondArr[j]);
                
                if (isRepeat) {//有重复
                    repeatIndex = j;
                    break;
                }
            }
            if (repeatIndex >= 0) {
                [repeatArr addObject:secondArr[repeatIndex]];
            }
        }
        [resuleArray addObjectsFromArray:secondArr];
        [resuleArray removeObjectsInArray:repeatArr];
        
    }
    [firstArr addObjectsFromArray:resuleArray];
    return firstArr;
}

+ (NSArray *)removeRepeatWithOldArr:(NSArray *)firstArr
                             addArr:(NSArray *)secondArr
                        compareDict:(NSString *(^)(id obj))compareKey
{
    if (!secondArr || secondArr.count <= 0) {
        return  firstArr;
    }else if (!firstArr || firstArr.count <= 0){
        return secondArr;
    }
    NSMutableArray * resultArray   = [NSMutableArray array];
    [resultArray addObjectsFromArray:firstArr];
    [resultArray addObjectsFromArray:secondArr];
    
    NSMutableArray *repeatArr = [NSMutableArray array];//保存筛选后的数组
    NSMutableDictionary *sortDict = @{}.mutableCopy;
    
    for (id subObj in resultArray) {
        NSString *tempKey = compareKey(subObj);
        id tempObj = [sortDict objectForKey:tempKey];
        if (!tempObj) {
            [repeatArr addObject:subObj];
            [sortDict setObject:subObj forKey:tempKey];
        }
    }

    return repeatArr.copy;
}

@end
